<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.phperskill.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Admin;

use Core\Db\Db as Db;

/**
 * 幻灯片
 * @author LazyPHP
 * @version 1.0
 */
class Slideshow extends Common {

    private $pic = array(
        'field_name' => 'slideshow_pic',
        'name' => '图片',
        'type' => 'thumb'
    );

    /**
     * 幻灯片位列表
     */
    public function index() {
        $db = $this->db('slideshow_type');
        $list = $db->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加幻灯片位
     */
    public function add() {
        $this->assign('title', '添加幻灯片');
        $this->assign('actionUrl', '/?g=Admin&m=Slideshow&a=insert');
        $this->display();
    }

    /**
     * 插入幻灯片位
     */
    public function insert() {
        $data['slideshow_type_name'] = $this->is_p('slideshow_type_name', '请填写幻灯片名称');
        $db = $this->db('slideshow_type');
        $addResult = $db->add($data);
        $this->determineSqlExecResult($addResult, '添加幻灯片失败');
        $this->success('成功添加幻灯片!', '/?g=Admin&m=Slideshow&a=index');
    }

    /**
     * 编辑幻灯片位
     */
    public function edit() {
        $id = $this->is_g('id', '请选择您要编辑的幻灯片');
        $db = $this->db('slideshow_type');
        $result = $db->where('slideshow_type_id = :slideshow_type_id')->find(array('slideshow_type_id' => $id));
        $this->determineSqlExecResult($result, '不存在此幻灯片!');
        foreach ($result as $key => $value) {
            $this->assign($key, $value);
        }
        $this->assign('title', '编辑幻灯片');
        $this->assign('actionUrl', '/?g=Admin&m=Slideshow&a=update&id=' . $id);
        $this->display('Slideshow_add');
    }

    /**
     * 更新幻灯片位
     */
    public function update() {
        $data['slideshow_type_id'] = $this->is_g('id', '请选择您要编辑的幻灯片');
        $data['slideshow_type_name'] = $this->is_p('slideshow_type_name', '请填写幻灯片名称');
        $db = $this->db('slideshow_type');
        $result = $db->where('slideshow_type_id = :slideshow_type_id')->save($data);
        $this->determineSqlExecResult($result, '幻灯片更新失败!');
        $this->success('幻灯片更新成功!', '/?g=Admin&m=Slideshow&a=index');
    }

    /**
     * 删除幻灯片位
     */
    public function delete() {
        $id = $this->is_g('id', '请选择您要删除的幻灯片');
        $db = $this->db('slideshow_type');
        $result = $db->where('slideshow_type_id = :slideshow_type_id')->delete(array('slideshow_type_id' => $id));
        $this->determineSqlExecResult($result, '删除幻灯片失败!');
        //删除对应的图片
        $db->tableName('slideshow');
        $existPictrue = $db->where('slideshow_type_id = :slideshow_type_id')->find(array('slideshow_type_id' => $id));
        if (!empty($existPictrue)) {
            $deletePictrue = $db->where('slideshow_type_id = :slideshow_type_id')->delete(array('slideshow_type_id' => $id));
            $this->determineSqlExecResult($deletePictrue, '清空图片失败!');
        }
        $this->success('幻灯片删除成功!', '/?g=Admin&m=Slideshow&a=index');
    }

    /**
     * 管理幻灯片图片
     */
    public function pictrue() {
        $slideshow_type_id = $this->existSlides();
        $db = $this->db('slideshow');
        $list = $db->where('slideshow_type_id = :slideshow_type_id')->order('listsort asc, slideshow_id desc')->select(array('slideshow_type_id' => $slideshow_type_id));
        $this->assign('list', $list);
        $this->display();
    }
    
    /**
     * 添加图片
     */
    public function pictrueAdd() {
        $slideshow_type_id = $this->existSlides();
        $this->assign('title', '添加图片');
        $this->assign('actionUrl', '/?g=Admin&m=Slideshow&a=pictrueInsert&slideshow_type_id=' . $slideshow_type_id);
        $this->assign('pic', $this->pic);
        $this->display();
    }
    
    /**
     * 插入图片
     */
    public function pictrueInsert() {
        $data['slideshow_type_id'] = $this->existSlides();

        $data['slideshow_name'] = $this->is_p('slideshow_name', '请填写名称');
        $data['slideshow_pic'] = $this->is_p('slideshow_pic', '请上传图片');
        $data['slideshow_link'] = $this->p('slideshow_link');
        $data['slideshow_description'] = $this->p('slideshow_description');
        $db = $this->db('slideshow');
        $addResult = $db->add($data);
        $this->determineSqlExecResult($addResult, '添加图片失败');
        $this->clearUploadRecord();
        $this->success('成功添加图片!', '/?g=Admin&m=Slideshow&a=pictrue&slideshow_type_id=' . $data['slideshow_type_id']);
    }
    
    /**
     * 编辑图片
     */
    public function pictrueEdit() {
        $data['slideshow_type_id'] = $this->existSlides();

        $id = $this->is_g('id', '请选择您要编辑的图片');
        $db = $this->db('slideshow');
        $result = $db->where('slideshow_id = :slideshow_id')->find(array('slideshow_id' => $id));
        $this->determineSqlExecResult($result, '不存在此图片!');
        foreach ($result as $key => $value) {
            $this->assign($key, $value);
        }
        $this->pic['value'] = $result['slideshow_pic'];
        $this->assign('pic', $this->pic);

        $this->assign('title', '编辑图片');
        $this->assign('actionUrl', '/?g=Admin&m=Slideshow&a=pictrueUpdate&slideshow_type_id=' . $data['slideshow_type_id'] . '&id=' . $id);
        $this->display('Slideshow_pictrueAdd');
    }
    
    /**
     * 更新图片
     */
    public function pictrueUpdate() {
        $data['slideshow_type_id'] = $this->existSlides();

        $data['slideshow_id'] = $this->is_g('id', '请选择您要编辑的图片');
        $data['slideshow_name'] = $this->is_p('slideshow_name', '请填写名称');
        $data['slideshow_pic'] = $this->is_p('slideshow_pic', '请上传图片');
        $data['slideshow_link'] = $this->p('slideshow_link');
        $data['slideshow_description'] = $this->p('slideshow_description');
        $db = $this->db('slideshow');
        $result = $db->where('slideshow_id = :slideshow_id')->save($data);
        $this->determineSqlExecResult($result, '图片更新失败!');
        $this->clearUploadRecord();
        $this->success('图片更新成功!', '/?g=Admin&m=Slideshow&a=pictrue&slideshow_type_id=' . $data['slideshow_type_id']);
    }
    
    /**
     * 删除图片
     */
    public function pictrueDelete() {
        $data['slideshow_type_id'] = $this->existSlides();
        $id = $this->is_g('id', '请选择您要删除的图片');
        $db = $this->db('slideshow');
        $result = $db->where('slideshow_id = :slideshow_id')->delete(array('slideshow_id' => $id));
        $this->determineSqlExecResult($result, '删除图片失败!');
        $this->success('删除图片成功!', '/?g=Admin&m=Slideshow&a=pictrue&slideshow_type_id=' . $data['slideshow_type_id']);
    }

    /**
     * 判断幻灯片位是否存在
     * @return type
     */
    private function existSlides() {
        $slideshow_type_id = $this->is_g('slideshow_type_id', '请选择您要管理的幻灯片');
        $db = $this->db('slideshow_type');
        $result = $db->where('slideshow_type_id = :slideshow_type_id')->find(array('slideshow_type_id' => $slideshow_type_id));
        $this->determineSqlExecResult($result, '不存在此幻灯片!');
        return $slideshow_type_id;
    }

    /**
     * @todo 此方法需要进行改进的！！
     */
    public function upload() {
        parent::upload();
    }

    /**
     * 进行排序
     */
    public function listsort() {
        $listsort = $this->is_p('listsort', '您没有提交值');

        $db = $this->db('slideshow');
        foreach ($listsort as $key => $value) {
            $db->where('slideshow_id = :slideshow_id')->save(array('slideshow_id' => $key, 'listsort' => $value));
        }
        $this->success('排序已更新!', '/?g=Admin&m=' . MODULE . '&a=index');
    }

}
